//
//  BSLproximity.m
//  Chamleon-template
//
//  Created by YLW on 15/10/16.
//
//

#import "BSLproximity.h"


@implementation CDVProximityData


- (instancetype)init
{
    if (self = [super init])
    {
        self.proximityStatus = PROXIMITYSTOPPED;
        self.proximityCallbacks = nil;
        self.proximityFilter = nil;
        self.proximityTimestamp = nil;
        self.proximityType = PROXITYNONE;
    }
    return self;
}

@end

@implementation BSLproximity



- (void)openProximity
{
    
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityStateChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateChange{
    BOOL status =  [UIDevice currentDevice].proximityState;
    CDVPluginResult *result = nil;
    if (status) {
        if (self.proximityData.proximityType == PROXITYNEAR) {
            return;
        }
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
        self.proximityData.proximityType = PROXITYNEAR;
        NSLog(@"近了");
    }else{
        if (self.proximityData.proximityType == PROXITYREMOTE) {
            return;
        }
        self.proximityData.proximityType =PROXITYREMOTE ;

        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Infinity"];
        NSLog(@"远了");
    }
    
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
    
    if (self.proximityData.proximityStatus == PROXIMITYSTARTING) {
        [self stopProximity];
    }
}

- (void)getCurrentProximity:(CDVInvokedUrlCommand *)command
{
    
    NSInteger leach = [[command argumentAtIndex:0 withDefault:nil] integerValue];
    NSLog(@"%zd",leach);
    if (leach)
    {
        self.proximityData.proximityStatus = PROXIMITYSTARTING;
    }
    else
    {
        self.proximityData.proximityStatus = PROXIMITYRUNNING;
    }
    self.callbackID = command.callbackId;
    [self openProximity];
}

- (void)watchProximity:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    self.proximityData.proximityStatus = PROXIMITYRUNNING;
    [self openProximity];
    
}

- (void)clearWatch:(CDVInvokedUrlCommand *)command
{
    [self stopProximity];
}

- (void)stopProximity
{
    [UIDevice currentDevice].proximityMonitoringEnabled = NO;
    self.proximityData.proximityStatus = PROXIMITYSTOPPED;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (CDVProximityData *)proximityData
{
    if (_proximityData == nil)
    {
        _proximityData = [[CDVProximityData alloc] init];
    }
    return _proximityData;
}

-(void)dealloc
{
    [UIDevice currentDevice].proximityMonitoringEnabled = NO;
    self.proximityData.proximityStatus = PROXIMITYSTOPPED;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}

@end
